package study;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class ImageViewer {
  public static void main(String[] args) {
    ImageViewerFrame imageViewer = new ImageViewerFrame();
    imageViewer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    imageViewer.setVisible(true);
  }
}

class ImageViewerFrame extends JFrame {
  public static final int WIDTH=800;
  public static final int HEIGHT=600;
  private JFileChooser jfc = null;
  JLabel image = null;
  
  public ImageViewerFrame() {
    this.setTitle("ImageViewer");
    this.setSize(WIDTH, HEIGHT);
    
    image = new JLabel();
    this.add(image);
    
    jfc = new JFileChooser();
    jfc.setCurrentDirectory(new File("d:\\software\\books"));
    
    JMenuBar jmb = new JMenuBar();
    this.setJMenuBar(jmb);
    
    JMenu fileMenu = new JMenu("File");
    jmb.add(fileMenu);
    
    JMenuItem open = new JMenuItem("Open");
    fileMenu.add(open);
    open.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int result = jfc.showOpenDialog(ImageViewerFrame.this);
        if(result == JFileChooser.APPROVE_OPTION) {
          String path = jfc.getSelectedFile().getAbsolutePath();
          image.setIcon(new ImageIcon(path));
        }
      }
    });
    
    JMenuItem exit = new JMenuItem("Exit");
    fileMenu.add(exit);
    exit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });
  }
}